package com.wholeintech.dao;

import java.util.List;

import com.wholeintech.entity.Scene;
import com.wholeintech.utils.DatabaseUtils;

import android.content.Context;

import org.xutils.DbManager;
import org.xutils.ex.DbException;


/**
 * 场景数据操作类
 *
 * @author Administrator
 */
public class SceneDao {
    private final static String TAG = "SceneDao";
    private Context mContext = null;

    public SceneDao(Context context) {
        this.mContext = context;
    }


    /**
     * 新增
     *
     * @param scene
     * @return
     */
    public boolean add(Scene scene) {
        boolean bRet = false;
        DbManager db = DatabaseUtils.DeviceDbUtils();
        try {
            db.save(scene);
            bRet = true;
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return bRet;
    }

    /**
     * 删除信息
     *
     * @param intId
     */
    public void delete(int intId) {
        DbManager db = DatabaseUtils.DeviceDbUtils();

        try {
            db.deleteById(Scene.class, intId);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 删除信息
     */
    public void deleteAll() {
        DbManager db = DatabaseUtils.DeviceDbUtils();

        try {
            //db.deleteAll(Scene.class);
            db.dropTable(Scene.class);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 获取所有场景
     *
     * @return
     */
    public List<Scene> getSceneList() {
        DbManager db = DatabaseUtils.DeviceDbUtils();
        List<Scene> sceneList = null;

        try {
            sceneList = db.findAll(Scene.class);
            if (sceneList != null) {
                sceneList.size();
            }

        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return sceneList;
    }
}
